﻿using HelixToolkit.Wpf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media.Media3D;

namespace Visualizer.HelixToolkitWpf
{
    /// <summary>
    /// Erweiterung von <see cref="HelixToolkit.Wpf.MeshBuilder"/>.
    /// </summary>
    class MeshBuilder2 : MeshBuilder
    {

        #region Constructor(s)

        /// <summary>
        /// Initializes a new instance of the <see cref="MeshBuilder"/> class.
        /// </summary>
        /// <remarks>
        /// Normal and texture coordinate generation are included.
        /// </remarks>
        public MeshBuilder2() : base(true, true) {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="MeshBuilder"/> class.
        /// </summary>
        /// <param name="generateNormals">
        /// Generate normal vectors.
        /// </param>
        /// <param name="generateTextureCoordinates">
        /// Generate texture coordinates.
        /// </param>
        public MeshBuilder2(bool generateNormals, bool generateTextureCoordinates)
            : base(generateNormals, generateTextureCoordinates) {
        }

        #endregion

        #region Methods

        /// <summary>
        /// Adds a tetrahedon
        /// </summary>
        /// <param name="center">
        /// The center.
        /// </param>
        /// <param name="sideLength">
        /// Length of the sides of the tetrahedon.
        /// </param>
        /// <param name="height">
        /// The height of the tetrahedon.
        /// </param>
        /// <remarks>
        /// See http://en.wikipedia.org/wiki/Tetrahedron.
        /// </remarks>
        public void AddTetrahedon(Point3D center, double sideLength, double height) {
            this.AddTetrahedon(center, new Vector3D(1, 0, 0), new Vector3D(0, 0, 1), sideLength, height);
        }

        /// <summary>
        /// Adds a tetrahedon.
        /// </summary>
        /// <param name="center">The center.</param>
        /// <param name="front">The normalized front vector</param>
        /// <param name="up">The normalized up vector</param>
        /// <param name="sideLength">Length of the side.</param>
        /// <param name="height">The height.</param>
        public void AddTetrahedon(Point3D center, Vector3D front, Vector3D up, double sideLength, double height) {
            double halfSide = sideLength / 2.0;
            var right = Vector3D.CrossProduct(front, up);
            var n = front * halfSide;
            up *= height;
            right *= halfSide;

            var p1 = center - n;
            var p2 = center + n + right;
            var p3 = center + n - right;
            var p4 = center + up;           // Spitze

            this.AddTriangle(p1, p2, p4);   // rechts
            this.AddTriangle(p2, p3, p4);   // vorne
            this.AddTriangle(p3, p1, p4);   // links
        }

        #endregion
    }
}
